/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var data = {};
function loadForOrigin() {
         var input = document.getElementById('origin');
         var autocomplete = new google.maps.places.Autocomplete(input);
}
google.maps.event.addDomListener(window, 'load', loadForOrigin);

function loadForDestiny() {
         var input = document.getElementById('destiny');
         var autocomplete = new google.maps.places.Autocomplete(input);
}
google.maps.event.addDomListener(window, 'load', loadForDestiny);
 
    var rendererOptions = {
        draggable: true
    };
    var merida = new google.maps.LatLng(20.978, -89.620);
    var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);;
    var directionsService = new google.maps.DirectionsService();
    var map;
  
 function drawRoute(origin, destiny)
 {
     var request = {
        origin: origin,
        destination: destiny,
        travelMode: google.maps.TravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
        }
  });
    
 }


$(function(){
    $("#destiny").focusout(function(){
        drawRoute($("#origin").val(),$("#destiny").val());
    });
    
    $('#newRound').submit(function(event){
                     event.preventDefault();
                     var w=[],wp;
                    var rleg = directionsDisplay.directions.routes[0].legs[0];
                    data.start = {'lat': rleg.start_location.lat(), 'lng':rleg.start_location.lng()}
                    data.end = {'lat': rleg.end_location.lat(), 'lng':rleg.end_location.lng()}
                    var wp = rleg.via_waypoints	
                    for(var i=0;i<wp.length;i++)w[i] = [wp[i].lat(),wp[i].lng()]	
                    data.waypoints = w;

                    var str = JSON.stringify(data);
                    $("#route").val(str);
                     $.post("RegisterRound", $("#newRound").serialize(), function(){
                          $.post("ListOwnerRounds", function(){
                                window.location.href = "myRounds.jsp";
                            });                       
                    });
                 })
                 
                 $('#editRound').submit(function(event){
                     event.preventDefault();
                     var w=[],wp;
                    var rleg = directionsDisplay.directions.routes[0].legs[0];
                    data.start = {'lat': rleg.start_location.lat(), 'lng':rleg.start_location.lng()}
                    data.end = {'lat': rleg.end_location.lat(), 'lng':rleg.end_location.lng()}
                    var wp = rleg.via_waypoints	
                    for(var i=0;i<wp.length;i++)w[i] = [wp[i].lat(),wp[i].lng()]	
                    data.waypoints = w;

                    var str = JSON.stringify(data);
                    $("#route").val(str);
                     $.post("UpdateRound", $("#editRound").serialize(), function(){
                          $.post("ListOwnerRounds", function(){
                                window.location.href = "myRounds.jsp";
                            });                       
                    });
                 })
    
    });
    


$(document).ready(function(){
    $("#newRound").validate({
        rules:{
            origin: {required:true},
            destiny: {required:true},
            schedule: {required:true},
            quota: {required:true},
            capacity:{required:true}
        },
        messages:{
            origin: "* Se requiere un origen",
            destiny: "* Se requiere un destino",
            schedule: "* Se requiere un horario",
            quota: "* Indique el costo",
            capacity:"* Indique el número de pasajeros"
        }
    });
    var myOptions = {
    zoom: 7,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: merida  
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
    drawRoute($("#origin").val(),$("#destiny").val());
});

